﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.Cache;
using Chinaer.Model.Common;
using Chinaer.Core.Helper;

namespace Chinaer.Bll
{
    public class MenuDeatil : Repository, IBll.IMenuDetail
    {
        Chinaer.IDao.IMenuDeatilDAO menuDetailDAO = new Dao.MenuDetailDAO();
        public DTO.MenuDetailDTO GetMenuDetail(string menuDetailID)
        {
            if (CacheHelper.Memcached_KeyExists("MenuDetail" + menuDetailID))
            {
                return (DTO.MenuDetailDTO)CacheHelper.Memcached_Get("MenuDetail" + menuDetailID);
            }
            Model.Entities.Model_MenuDetail model_MenuDeail = menuDetailDAO.GetByID(menuDetailID);
            DTO.MenuDetailDTO menuDetailDTO = new DTO.MenuDetailDTO();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_MenuDeail, menuDetailDTO);
            CacheHelper.Memcached_Set("MenuDetail" + menuDetailID, menuDetailDTO);

            return menuDetailDTO;
        }

        public List<DTO.MenuDetailDTO> GetMenuDetailByMenuCategory(string menuCategoryID)
        {
            if (CacheHelper.Memcached_KeyExists("GetMenuDetailByMenuCategory" + menuCategoryID))
            {
                return (List<DTO.MenuDetailDTO>)CacheHelper.Memcached_Get("GetMenuDetailByMenuCategory" + menuCategoryID);
            }
            List<Model.Entities.Model_MenuDetail> listMenuDetail = menuDetailDAO.GetMenuDetailByMenuCategoryID(menuCategoryID);
            List<DTO.MenuDetailDTO> listMenuDetailDTO = new List<DTO.MenuDetailDTO>();
            foreach (var item in listMenuDetail)
            {
                DTO.MenuDetailDTO menuDetailDTO = new DTO.MenuDetailDTO();
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(item, menuDetailDTO);
                listMenuDetailDTO.Add(menuDetailDTO);
            }
            CacheHelper.Memcached_Set("GetMenuDetailByMenuCategory" + menuCategoryID, listMenuDetailDTO);

            return listMenuDetailDTO;
        }

        public JsonResult Add(DTO.MenuDetailDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            JsonResult jsonResult = new JsonResult();
            Model.Entities.Model_MenuDetail model_MenuDetail = new Model.Entities.Model_MenuDetail();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_MenuDetail);
            if (string.IsNullOrEmpty(model_MenuDetail.ID) || model_MenuDetail.ID.Trim() == "")
            {
                model_MenuDetail.ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID();
            }
            jsonResult.Success = menuDetailDAO.Add(model_MenuDetail);
            return jsonResult;
        }

        public JsonResult Delete(DTO.MenuDetailDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            JsonResult jsonResult = new JsonResult();
            Model.Entities.Model_MenuDetail model_MenuDetail = new Model.Entities.Model_MenuDetail();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_MenuDetail);
            jsonResult.Success = menuDetailDAO.Delete(model_MenuDetail);
            return jsonResult;
        }

        public JsonResult Delete(string id)
        {
            JsonResult jsonResult = new JsonResult();
            jsonResult.Success = menuDetailDAO.Delete(id);
            return jsonResult;
        }

        public JsonResult Update(DTO.MenuDetailDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            JsonResult jsonResult = new JsonResult();
            Model.Entities.Model_MenuDetail model_MenuDetail = new Model.Entities.Model_MenuDetail();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_MenuDetail);
            jsonResult.Success = menuDetailDAO.Update(model_MenuDetail);
            return jsonResult;
        }


        public List<DTO.MenuDetailDTO> GetMenuDetailByDianPuID(string dianPuID)
        {
            List<Model.Entities.Model_MenuDetail> model_menuDetailList = menuDetailDAO.GetMenuDetailByDianPuID(dianPuID);
            List<DTO.MenuDetailDTO> menuDetailDTOList = new List<DTO.MenuDetailDTO>();
            foreach (Model.Entities.Model_MenuDetail item in model_menuDetailList)
            {
                DTO.MenuDetailDTO menuDetailDTO = new DTO.MenuDetailDTO();
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(item, menuDetailDTO);
                menuDetailDTOList.Add(menuDetailDTO);
            }
            return menuDetailDTOList;
        }


        public DTO.MenuDetailDTO GetByID(string id)
        {
            Model.Entities.Model_MenuDetail model_MenuDetail = new Model.Entities.Model_MenuDetail();
            DTO.MenuDetailDTO menuDetailDTO = new DTO.MenuDetailDTO();
            model_MenuDetail = menuDetailDAO.GetByID(id);
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_MenuDetail, menuDetailDTO);
            return menuDetailDTO;
        }


        public List<DTO.MenuDetailDTO> GetAllMenuDetail()
        {
            List<Model.Entities.Model_MenuDetail> model_menuDetailList = menuDetailDAO.GetAllMenuDetail();
            List<DTO.MenuDetailDTO> menuDetailDTOList = new List<DTO.MenuDetailDTO>();
            foreach (Model.Entities.Model_MenuDetail item in model_menuDetailList)
            {
                DTO.MenuDetailDTO menuDetailDTO = new DTO.MenuDetailDTO();
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(item, menuDetailDTO);
                menuDetailDTOList.Add(menuDetailDTO);
            }
            return menuDetailDTOList;
        }
    }
}
